﻿@model IEnumerable<Citi.GocAttributes.WebUI.Models.RuleModel>
@{
    ViewBag.Title = "Rules";
}
@section Menu
{
    @Html.Partial("~/Views/Menu/Rules.cshtml", new ViewDataDictionary())
}
<script type="text/javascript">
    var uniqueId = '@Guid.NewGuid().ToString()';

    function openCloseRuleDialog() {
        $('#ruleCreate').modal('toggle');

        $('#buttonModalAction').unbind("click");

        $('#buttonModalAction').click(function () {
            $('#submitRuleForm').click();
        });
    }

    function openCloseApplyRuleDialog() {
        $('#applyRuleModal').removeData();

        $('#buttonApplyRule').show();
        $('#buttonCancelApplyRule').text('Cancel');

        $('#applyRuleModal').modal('toggle');
        $('#filterApplyRule').show();
        $('#applyRuleLoading').hide();
        $('#loading').hide();

        $('#buttonApplyRule').click(function (event) {
            $.post('@Url.Action("StartApplyRules")', { id: uniqueId, month: $('#ApplyRuleMonth').val(), year: $('#ApplyRuleYear').val() }, function () {
                $('#applyRuleLoading').show();
                $('#loading').show();
                $('#currentStatus').text('');
                $('#buttonApplyRule').attr('disabled', 'disabled');
                getStatus();
            });
            event.preventDefault;
        });
    }

    var lastStatus = '';

    function getStatus() {
        var url = '@Url.Action("GetCurrentProgress")/' + uniqueId;
        $.get(url, function (data) {
            //$('#applyRulePercent').html(data);

            if (data != "Rules successfully applied!") {
                if (data != lastStatus) {
                    $('#currentStatus').prepend('<p>' + data + '</p>');
                    lastStatus = data;
                    $('#statusApply').scrollTop(0);
                }

                window.setTimeout("getStatus()", 100);
            }
            else {
                $('#currentStatus').prepend('<p>' + data + '</p>');
                $('#loading').hide();
                alert("Rules successfully applied!");
                $('#buttonApplyRule').removeAttr('disabled');
                $('#buttonApplyRule').hide();
                $('#buttonCancelApplyRule').text('  Done  ');
            };
        });
    }

    $(function () {
        $("#ruleOpenForm").html('<i class="icon-plus"></i>&nbsp;&nbsp;&nbsp;Create new');
    });
</script>
<h2>
    Rules</h2>
@Ajax.ActionLink("Create new", "Form",
    new { id = (int?)null },
                        new AjaxOptions { UpdateTargetId = "ruleForm", OnBegin = "openCloseRuleDialog" },
    new { @class = "btn", id = "ruleOpenForm" })
<button class="btn" onclick="openCloseApplyRuleDialog();">
    <i class="icon-list"></i>&nbsp;&nbsp;&nbsp; Apply Rules
</button>
<br />
<br />
<div id="ruleList">
    @Html.Partial("~/Views/Rule/List.cshtml")
</div>
<div id="ruleCreate" class="modal hide" style="width: 650px; max-height:95%; margin-left: -300px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">
            ×</button>
    </div>
    <div class="modal-body" id="ruleForm" style="max-height:350px">
    </div>
    <div class="modal-footer">
        @if (Request.QueryString["Details"] == null)
        {
            <a href="#" id="buttonModalAction" class="btn btn-primary">Save</a>
        }
        <a href="#" class="btn" data-dismiss="modal">Cancel</a>
    </div>
</div>
<div id="applyRuleModal" class="modal hide" style="width: 650px; margin-left: -300px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">
            ×</button>
    </div>
    <div class="modal-body">
        <div id="filterApplyRule" style="display: none; height: 200px;">
            <h4>
                Select the date to apply rules</h4>
            Month:
            <select id="ApplyRuleMonth">
                <option value="@DateTime.Today.AddMonths(-1).Month">@DateTime.Today.AddMonths(-1).ToString("MMMM")</option>
                <option selected="selected" value="@DateTime.Today.Month">@DateTime.Today.ToString("MMMM")</option>
                <option value="@DateTime.Today.AddMonths(1).Month">@DateTime.Today.AddMonths(1).ToString("MMMM")</option>
            </select>
            Year:
            <select id="ApplyRuleYear">
                @if (DateTime.Today.Month == 1)
                {
                    <option value="@DateTime.Today.AddYears(-1).Year">@DateTime.Today.AddYears(-1).Year</option>
                }
                <option selected="selected" value="@DateTime.Today.Year">@DateTime.Today.Year</option>
                @if (DateTime.Today.Month == 12)
                {
                    <option value="@DateTime.Today.AddYears(1).Year">@DateTime.Today.AddYears(1).Year</option>
                }
            </select>
            <div id="applyRuleLoading" style="margin-top: 15px; display: none;">
                <div style="margin-bottom: 10px">
                    <center>
                        <img src="@Url.Content("~/Content/Images/loading-bar.gif")" alt="Applying rules" id="loading"/></center>
                </div>
                <div id="statusApply" style="width: 100%; height: 100px; overflow-y: scroll">
                    <span id="currentStatus"></span>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn btn-primary" id="buttonApplyRule">Apply</a> 
        <a href="#" class="btn" data-dismiss="modal" id="buttonCancelApplyRule">Cancel</a>
    </div>
</div>
